%=============================================================================================
%Calibration of the model with Imperfect Competitive Deposit Market and
%Competitive lending market
% Copy right: Yu Zhu
%=============================================================================================

%clear
close all
%% Read Lucas-Nicolini data
data = importdata('lucas_nicolini.csv');
rate_data = importdata('deposit_rates.csv');
caopt = importdata('ssopt.txt');
data_final = data.data;
rates = rate_data.data;
omega_grid = importdata('omega.txt');

alpha1 = omega_grid.data(1);
alpha2 = omega_grid.data(2);
alpha3 = omega_grid.data(3);


% Define years used in calibration
first_year = caopt.data(1);
last_year = caopt.data(2);
%options for steady sate
ssopt = caopt.data(3); 
theta_low = caopt.data(4);

%=====================================
% Scatter plot of Lucas-Nicolini data
%=====================================
% LNfigure=figure
% hold on
% ID1 = data.data(:,1)>=1983;
% ID2 = data.data(:,1)<=1935;
% ID3 = logical(1-ID1-ID2);
 ID4 =  data.data(:,1)>=first_year&data.data(:,1)<=last_year;
% scatter(data.data(ID2,5),data.data(ID2,4),'red')
% scatter(data.data(ID3,5),data.data(ID3,4),'blue')
% scatter(data.data(ID1,5),data.data(ID1,4),'green')
% 
% legend( 'Before 1934','1935-1983','After 1982')
% xlabel('3m T-Bill Rate')
% ylabel('M1 to GDP Ratio')
% figure_halfpage
% saveas(LNfigure,'figures/money_demand_data','epsc')
% saveas(LNfigure,'figures/money_demand_data','png')

%============================================================================
% Restrict data period to before crisis and not immediately after 911
%============================================================================
% prepare data
data = [data_final(:,5)/100,data_final(:,4),data_final(:,1)];
data = data(ID4,:); % using regulated period with deposit rate data
ID5=rates(:,1)>=first_year&rates(:,1)<=last_year;
data = [data, rates(ID5,2),rates(ID5,3),rates(ID5,4)/100];

data = sortrows(data,1);



%============================================================================
% define variables
%============================================================================
pi_m=0.0312; % annual inflation

i_l = 0.0588555;

i_D=0.0073; % annual rate for demand deposits

online=0.2537; 
%from online shopping fraction in the survery of
%consumer payment choice Atlanta fed

epsilon = 0.001; % ensures the DM utility function is 0 at 0


eta = 0.66;  %Poduction function of firms f(k)=A*k^(eta)

beta = 0.96; % annual discount factor


rq_ratio = 0.1;
%================================================
%pass variables
%================================================
otherpar.beta=beta;
otherpar.epsilon = epsilon;
otherpar.pi_m=pi_m;
otherpar.spread = i_l-i_D;
otherpar.i_D = i_D;
otherpar.i_l = i_l;
otherpar.alpha1=alpha1;
otherpar.alpha2=alpha2;
otherpar.alpha3= alpha3;
otherpar.rq_ratio=rq_ratio;
otherpar.eta=eta;
%=======================================================================
%calibration
%=======================================================================

ppp0 = [1.5,.95442];
tradep=importdata('tradep.txt');
pppl = [1.4,0.9];
ppph = [1.6,0.99];
%calibrate_money_demand_fuc(.9,tradep,data)
ppp = fzero(@(x)calibrate_money_demand_fuc(x,tradep,otherpar,data,ssopt),[theta_low,1]);
[yyy, output,pars, model]=calibrate_money_demand_fuc(ppp,tradep,otherpar,data,ssopt);
WW = [pars,output,tradep];
fileID = fopen('output.txt','w');
fprintf(fileID,'%12.8f ',WW);
fprintf(fileID,'\n');
fclose(fileID);

